<template>
  <el-container class="home-container">
    <el-header height="80px">
      <div>
        <img src="../assets/logo.png" alt="" />
        <h2>电商后台管理系统</h2>
      </div>
      <el-button @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="asideWidth + 'px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 另一种写法 -->
        <!-- :unique-opened="true" -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409bff"
          unique-opened
          :collapse="isCollapse"
          router
          @select="handleSelect"
          :default-active="activePath"
        >
          <el-submenu
            :index="item.id.toString()"
            v-for="item in menuList"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item
              :index="'/' + item2.path"
              v-for="item2 in item.children"
              :key="item2.id"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ item2.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      // 左侧菜单一级图标
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      asideWidth: 200,
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = sessionStorage.getItem('activePath')
  },
  /* mounted() {
    this.getMenuList()
  }, */
  methods: {
    logout() {
      sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
      if (this.isCollapse) {
        this.asideWidth = 56
      } else {
        this.asideWidth = 200
      }
    },
    handleSelect(key, keyPath) {
      // console.log(key, keyPath)
      sessionStorage.setItem('activePath', key)
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  > div {
    display: flex;
    align-items: center;
    img {
      width: 50px;
      height: 50px;
      border-radius: 50%;
    }
    h2 {
      color: white;
      margin-left: 10px;
    }
  }
}
.el-aside {
  background: #333744;
  overflow: hidden;
  transition: width 1s;
  .toggle-button {
    line-height: 24px;
    background: #4a5064;
    text-align: center;
    font-size: 10px;
    color: white;
    cursor: pointer;
  }
  .el-menu {
    border-right: none; // 去掉右侧对不齐的bug
  }
  .el-menu:not(.el-menu--collapse) {
    width: 200px;
  }
  .iconfont {
    font-size: 18px;
    margin-right: 5px;
  }
}
.el-main {
  background: #eaedf1;
}
</style>
